package com.hongxun.design.adapters;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.MaterialDetailActivity;
import com.hongxun.design.activitys.ShowBigImageActivity;
import com.hongxun.design.bean.CodeSetBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/5/23.
 */
public class CodeSetModelAdapter extends BaseAdapter {
    public List<CodeSetBean> data;
    Context context;
    public Map<Integer, Boolean> map;
    LayoutInflater inflater;
    private String click, codeset_id;
    private boolean isJG = false;
    private Handler handler;

    public CodeSetModelAdapter(List<CodeSetBean> data, Context context) {
        this.context = context;
        this.data = data;
        map = new HashMap<>();
        for (int i = 0; i < data.size(); i++) {
            map.put(i, false);
        }
        inflater = LayoutInflater.from(context);
    }

    public Object getChecked() {
        List<CodeSetBean> back = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (map.get(i)) {
                back.add(data.get(i));
            }
        }
        return back;
    }

    public void setClick(String click) {
        this.click = click;
    }

    public void setCodeset_id(String codeset_id) {
        this.codeset_id = codeset_id;
    }

    public void setJG(boolean JG) {
        isJG = JG;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final CodeSetBean bean = data.get(i);
        Hollder hollder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_item_codesetmodel, null);
            hollder = new Hollder(view);
            view.setTag(hollder);
        } else {
            hollder = (Hollder) view.getTag();
        }
        hollder._title.setText(bean.getName());
        hollder._icon.setImageURI(UriUtil.getUri(bean.getThumb()));
        switch (click) {
            case MString.click_in:
                if (MyApp.NetWorkConnect) hollder._detailed.setVisibility(View.VISIBLE);
                else {
                    hollder._detailed.setVisibility(View.GONE);
                    break;
                }
                hollder._detailed.setOnClickListener(new MClickListener(i, bean) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        Bundle bundle = new Bundle();
                        if (isJG) bundle.putInt(MString.TYPE, Constant.EdMeterial);
                        else bundle.putInt(MString.TYPE, Constant.NOTEDIT);
                        bundle.putString(MString.codeset_id, ((CodeSetBean) msg).getCodeset_id());
                        IntentToActivity.intent((Activity) context, MaterialDetailActivity.class, bundle);
                    }
                });
                break;
            case MString.click_out:
                if (isJG) {
                    hollder._detailed.setOnClickListener(new MClickListener(i, bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            //明细 MaterialDetailActivity
                            Bundle bundle = new Bundle();
                            bundle.putInt(MString.TYPE, Constant.EdMeterial);
                            bundle.putString(MString.codeset_id, ((CodeSetBean) msg).getCodeset_id());
                            IntentToActivity.intent((Activity) context, MaterialDetailActivity.class, bundle);
                        }
                    });
                } else {
                    hollder._button.setOnClickListener(new MClickListener(i, bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            Bundle bundle = new Bundle();
                            bundle.putInt(MString.TYPE, Constant.NOTEDIT);
                            bundle.putString(MString.codeset_id, ((CodeSetBean) msg).getCodeset_id());
                            IntentToActivity.intent((Activity) context, MaterialDetailActivity.class, bundle);
                        }
                    });
                    //加入项目
                    hollder._detailed.setOnClickListener(new MClickListener(i, bean) {
                        @Override
                        public void onClick(View view, int position, Object msg) {
                            Message message = new Message();
                            message.what = 99;
                            message.obj = position;
                            handler.sendMessage(message);
                        }
                    });
                }
                break;
        }
        if (click.equals(MString.click_out)) {
            hollder._checked.setVisibility(View.GONE);
        } else {
            if (codeset_id != null && bean.getCodeset_id().equals(codeset_id)) {
                map.put(i, true);
                codeset_id = null;
            }
            hollder._checked.setOnClickListener(new MClickListener(i, bean) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    for (int i = 0; i < data.size(); i++) {
                        map.put(i, false);
                    }
                    if (((CheckBox) view).isChecked()) {
                        ((CheckBox) view).setChecked(true);
                        map.put(position, true);
                    } else {
                        ((CheckBox) view).setChecked(false);
                        map.put(position, false);
                    }
                    notifyDataSetChanged();
                }
            });
            boolean res;
            if (map.get(i) == null || !map.get(i)) {
                res = false;
                map.put(i, false);
            } else
                res = true;
            hollder._checked.setChecked(res);
        }
        hollder._icon.setOnClickListener(new MClickListener(i, bean) {
            @Override
            public void onClick(View view, int position, Object msg) {
                Bundle bundle = new Bundle();
                bundle.putString(MString.PATH, bean.getOriginal());
                IntentToActivity.intent((Activity) context, ShowBigImageActivity.class, bundle);
            }
        });
        return view;
    }

    class Hollder {
        LinearLayout _button;
        CheckBox _checked;
        SimpleDraweeView _icon;
        TextView _title, _detailed;

        Hollder(View view) {
            _button = (LinearLayout) view.findViewById(R.id.codesetmodel_button);
            _checked = (CheckBox) view.findViewById(R.id.codesetmodel_checked);
            _icon = (SimpleDraweeView) view.findViewById(R.id.codesetmodel_icon);
            _title = (TextView) view.findViewById(R.id.codesetmodel_title);
            _detailed = (TextView) view.findViewById(R.id.codesetmodel_detailed);

            if (click.equals(MString.click_out)) {
                if (isJG) {//项目竣工典设列表、
                    SpannableString styledText1 = new SpannableString(IconSourse.DETAILED);
                    styledText1.setSpan(new TextAppearanceSpan(context, R.style.style1), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    SetTypeface.set(_detailed, styledText1);
                    _detailed.append(MString.Detailed);
                } else {//选择平台典型组装
                    SpannableString styledText1 = new SpannableString(IconSourse.add);
                    styledText1.setSpan(new TextAppearanceSpan(context, R.style.style1), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    SetTypeface.set(_detailed, styledText1);
                    _detailed.append(" 加入项目");
                }
            } else {//点位选典设
                SpannableString styledText1 = new SpannableString(IconSourse.DETAILED);
                styledText1.setSpan(new TextAppearanceSpan(context, R.style.style1), 0, styledText1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                SetTypeface.set(_detailed, styledText1);
                _detailed.append(MString.Detailed);
            }
        }
    }
}
